<template>
  <div class="infinite-list">
    <el-row :gutter="20">
      <el-empty :image-size="200" v-if="commodityList.length===0" description="暂无商品信息" style="height: 88vh;width: 100%" />
      <el-col
          v-for="(item) in commodityList"
          :key="item"
          :span="6"
      >
        <el-card :body-style="{ padding: '0px' }" shadow="hover" @click="gotoDetails(item.uuid)">
          <el-image :src="item.img" fit="cover" class="card-img">
            <template #error>
              <div class="image-slot">
                <el-icon><picture /></el-icon>
              </div>
            </template>
          </el-image>
          <div class="card-info">
            <span>品名：{{item.name}}</span>
            <span>售价：￥ {{item.price}}</span>
            <span>上架日期：{{item.time}}</span>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <div style="text-align: right"></div>
    <el-pagination
        v-model:page-size="pageSize"
        v-model:currentPage="pageNum"
        @current-change="load"
        layout="prev, pager, next"
        :total="total"
        background
    />
  </div>
</template>

<script>
import {getCurrentInstance} from "vue";
import request from "@/http/request";
import router from "@/router";

export default {
  name: "FarmCropsHome",
  data() {
    return {
      posterList: [],
      loading: false,
      disabled: false,
      commodityList: [],
      pageNum: 1,
      pageSize: 12,
      total: 0,
      pages: (-1),
    }
  },
  methods: {
    load() {
      if (this.loading === false) {
        this.loading = true;
        if (this.pages>0 && this.pageNum > this.pages) {
          this.$tipUtil.infoTip("已为您加载所有商品信息");
          this.disabled = true;
        } else {
          request({
            "url": "commodity/getCommodityList",
            "method": "get",
            params: {
              pageNum: this.pageNum,
              pageSize: this.pageSize,
            }
          }).then((response) => {
            if (response.code === 200) {
              const _this = this;
              this.total = Number(response.data[0].total);
              this.pages = Number(response.data[0].pages);
              this.commodityList = response.data[0].data.map(function (item) {
                item.img = _this.staticResourcesUrl + item.img;
                return item;
              });
            } else if (response.code === 230) {
              this.$tipUtil.warningTip(response.msg);
            } else {
              this.$tipUtil.errTip(response.msg);
            }
            this.loading = false;
          })
        }
      }
    },
    gotoDetails(uuid) {
      router.push({
        name: "CommodityDetails",
        params: {
          uuid: uuid,
        }
      })
    },
  },
  setup() {
    const { appContext : { config: { globalProperties } } } = getCurrentInstance();
    const staticResourcesUrl = globalProperties.$staticResourcesUrl;
    return {
      staticResourcesUrl,
    }
  },
  mounted() {
    this.load();
  }
}
</script>

<style scoped>
.infinite-list {
  height: 93vh;
  margin: 0;
  padding: 20px 10px 0;
  overflow: auto;
  box-sizing: border-box;
}
.el-col{
  margin-bottom: 20px;
}
.el-col:last-child{
  margin-bottom: 0;
}
.card-img {
  height: 180px;
  min-width: 100%;
}
.card-info {
  height: 70px;
  padding: 7px 0;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
}
.card-info > span:nth-child(1){
  font-size: 16px;
  font-weight: 700;
  color: #53a8ff;
}
.card-info > span:nth-child(2){
  font-size: 14px;
  color: #ff6363;
}
.card-info > span:nth-child(3){
  font-size: 14px;
  color: #7a7a7a;
}
.el-pagination {
  width: 500px;
}
</style>